<template>
  <ContentWrap>
    <!-- 搜索栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="-mb-15px"
      label-width="90px"
    >
      <el-form-item label="下载用途" prop="name">
        <el-input
          v-model="queryParams.objective"
          class="!w-240px"
          clearable
          placeholder="请输入下载用途名称"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下载目的" prop="name">
        <el-input
          v-model="queryParams.useTo"
          class="!w-240px"
          clearable
          placeholder="请输入下载目的"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh" />
          重置
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="编号" prop="id" />
      <el-table-column align="center" label="下载目的" prop="objective" />
      <el-table-column align="center" label="下载用途" prop="useTo" />
      <el-table-column align="center" label="下载用途" prop="useTo" />
      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.DOWN_APP_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="detail(scope.row.processInstanceId)">
            查看
          </el-button>
          <el-button v-if="scope.row.status == 10" link type="primary" @click="down(scope.row)">
            下载
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNo"
      :total="total"
      @pagination="handleQuery"
    />
  </ContentWrap>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import * as downApi from '@/api/down'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
const router = useRouter() // 路由
import download from '@/utils/download'

const queryFormRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null,
  createTime: []
})
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据

const handleQuery = async () => {
  loading.value = true
  let data = await downApi.page(queryParams)
  list.value = data.list
  total.value = data.total
  loading.value = false
}
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const detail = (processInstanceId) => {
  router.push({
    name: 'BpmProcessInstanceDetail',
    query: {
      id: processInstanceId
    }
  })
}
const down = async (row) => {
  const res = await downApi.download(row.id)
  download.zip(res, new Date().getTime() + '.zip')
}
onMounted(() => {
  handleQuery()
})
</script>
